package org.dochandler.esfinge.guardian.authorizer;

import org.dochandler.service.domain.annotation.WorkingTime;
import org.dochandler.service.domain.constants.Constants;
import org.dochandler.service.domain.mission.AccessTime;
import org.esfinge.guardian.authorizer.Authorizer;
import org.esfinge.guardian.context.AuthorizationContext;

public class WorkingTimeAuthorizer implements Authorizer<WorkingTime> {

	private Integer WORKING_HOUR_START = 8;
	private Integer WORKING_MINUTES_START = 0;
	private Integer WORKING_SECONDS_START = 0;
	
	private Integer WORKING_HOUR_END = 17;
	private Integer WORKING_MINUTES_END = 59;
	private Integer WORKING_SECONDS_END = 59;
	
	@Override
	public Boolean authorize(AuthorizationContext context, WorkingTime securityAnnotation) {
		
		AccessTime time = context.getEnvironment().get( Constants.ACCESS_TIME.val(), AccessTime.class );
		
		boolean isEqualOrHigherThanWorkingStart = 
				(time.getHour() >= WORKING_HOUR_START &&
				 time.getMinutes() >= WORKING_MINUTES_START &&
				 time.getSeconds() >= WORKING_SECONDS_START);
		
		boolean isEqualOrLowerThanWorkingEnd = 
				(time.getHour() <= WORKING_HOUR_END &&
				 time.getMinutes() <= WORKING_MINUTES_END &&
				 time.getSeconds() <= WORKING_SECONDS_END);
		
		if (isEqualOrHigherThanWorkingStart && isEqualOrLowerThanWorkingEnd) {
			return true;
		} else { 
			return false;
		}
		
	}

}